﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 自动压缩备份
{
    public partial class SettingForm : Form
    {
        //自动备份时间点
        DateTime thisbackuptime = DateTime.MinValue;
        //自动删除时间间隔天数
        string thisdeletedaysstr = "";
        //是否开机启动
        bool thisisstartrun = false;
        //是否自动删除
        bool thisisontimedelete = false;
        //是否自动备份
        bool thisisontimebackup = false;
        //是否启动隐藏界面
        bool thisishideform = false;

        //记录设置窗体位置
        Point startpoint = Point.Empty;

        public SettingForm(DateTime backuptime, string deletedaysstr, bool isstartrun, bool isontimedelete, bool isontimebackup, bool ishideform)
        {
            InitializeComponent();

            thisbackuptime = backuptime;
            thisdeletedaysstr = deletedaysstr;
            thisisstartrun = isstartrun;
            thisisontimedelete = isontimedelete;
            thisisontimebackup = isontimebackup;
            thisishideform = ishideform;

            init();

            //记录设置窗体位置
            startpoint = this.Location;
        }

        /// <summary>
        /// 初始化设置界面
        /// </summary>
        private void init()
        {
            BackUpdateTimePicker.Value = thisbackuptime;
            switch (thisdeletedaysstr)
            {
                case "小三天":
                    comboxDeleteTime.SelectedIndex = 1;
                    break;
                case "小七天":
                    comboxDeleteTime.SelectedIndex = 2;
                    break;
                case "十五天":
                    comboxDeleteTime.SelectedIndex = 3;
                    break;
                case "一个月":
                    comboxDeleteTime.SelectedIndex = 4;
                    break;
                case "三个月":
                    comboxDeleteTime.SelectedIndex = 5;
                    break;
                default:
                    comboxDeleteTime.SelectedIndex = 0;
                    break;
            }

            chkStartRun.Checked = thisisstartrun;
            chkOnTimeBackUp.Checked = thisisontimebackup;
            if (thisisontimebackup)
            {
                BackUpdateTimePicker.Enabled = false;
            }
            chkOnTimeDelete.Checked = thisisontimedelete;
            if (thisisontimebackup)
            {
                comboxDeleteTime.Enabled = false;
            }
            ChkHideForm.Checked = thisishideform;

        }

        /// <summary>
        /// 设置是否开机启动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void chkStartRun_CheckedChanged(object sender, EventArgs e)
        {
            string path = Application.ExecutablePath;

            if (chkStartRun.Checked)
            {
                Properties.Settings.Default.IsStartRun = true;
                Properties.Settings.Default.Save();

                RegistryKey rk = Registry.LocalMachine.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
                if (rk.GetValue("BackUpProgram") == null)
                {
                    rk.SetValue("BackUpProgram", path);
                }
            }
            else
            {
                Properties.Settings.Default.IsStartRun = false;
                Properties.Settings.Default.Save();

                RegistryKey rk = Registry.LocalMachine.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
                if (rk.GetValue("BackUpProgram") != null)
                {
                    rk.DeleteValue("BackUpProgram");
                }
            }
        }

        /// <summary>
        /// 设置是否定时备份
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void chkOnTimeBackUp_CheckedChanged(object sender, EventArgs e)
        {
            if (chkOnTimeBackUp.Checked)
            {
                Properties.Settings.Default.BackUpTime = BackUpdateTimePicker.Value.ToString();
                Properties.Settings.Default.IsOnTimeBackUp = true;
                Properties.Settings.Default.Save();
                BackUpdateTimePicker.Enabled = false;
            }
            else
            {
                Properties.Settings.Default.IsOnTimeBackUp = false;
                Properties.Settings.Default.Save();
                BackUpdateTimePicker.Enabled = true;
            }
        }

        /// <summary>
        /// 设置是否定时删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void chkOnTimeDelete_CheckedChanged(object sender, EventArgs e)
        {
            if (chkOnTimeDelete.Checked)
            {
                Properties.Settings.Default.DeleteTime = comboxDeleteTime.SelectedText.ToString();
                Properties.Settings.Default.IsOnTimeDelete = true;
                Properties.Settings.Default.Save();
                BackUpdateTimePicker.Enabled = false;
            }
            else
            {
                Properties.Settings.Default.IsOnTimeDelete = false;
                Properties.Settings.Default.Save();
                BackUpdateTimePicker.Enabled = true;
            }
        }

        /// <summary>
        /// 修改删除时间间隔
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboxDeleteTime_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboxDeleteTime.SelectedItem.ToString() != Properties.Settings.Default.DeleteTime || Properties.Settings.Default.DeleteTime == "")
            {
                if (comboxDeleteTime.SelectedItem != null)
                {
                    Properties.Settings.Default.DeleteTime = comboxDeleteTime.SelectedItem.ToString();
                    Properties.Settings.Default.Save();
                }
            }
        }

        /// <summary>
        /// 设置是否启动隐藏界面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ChkHideForm_CheckedChanged(object sender, EventArgs e)
        {
            if (ChkHideForm.Checked)
            {
                Properties.Settings.Default.IsHideForm = true;
                Properties.Settings.Default.Save();
            }
            else
            {
                Properties.Settings.Default.IsHideForm = false;
                Properties.Settings.Default.Save();
            }
        }

        const int WM_NCLBUTTONDOWN = 0x00A1;
        const int HTCAPTION = 2;
        protected override void WndProc(ref Message m)
        {
            //禁止拖动
            if (m.Msg == WM_NCLBUTTONDOWN && m.WParam.ToInt32() == HTCAPTION)
                return;
            base.WndProc(ref m);
        }

        
    }
}
